package com.zhida.smartcontrol.pagectrl.lampmanager.doublelamp;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.jay.tools.base.BaseDialogFragment;
import com.jay.tools.base.BaseFragment;
import com.jay.tools.recycler.OnItemClick;
import com.jay.tools.ui.Cbox;
import com.jay.tools.utils.ResUtil;
import com.zhida.smartcontrol.R;
import com.zhida.smartcontrol.common.Const;
import com.zhida.smartcontrol.common.bean.LampBean;
import com.zhida.smartcontrol.common.bean.LampSwitchBean;
import com.zhida.smartcontrol.mvp.MVPBaseFragment;
import com.zhida.smartcontrol.dialog.deldevicedialog.DelDeviceDIalogFragment;
import com.zhida.smartcontrol.dialog.dimmerdialog.DimmerDialogFragment;
import com.zhida.smartcontrol.dialog.lampkzlxdialog.LampKzlxDialogFragment;
import com.zhida.smartcontrol.dialog.lampsjjzdialog.LampSjjzDialogFragment;
import com.zhida.smartcontrol.dialog.nbdialog.NBDialogFragment;
import com.zhida.smartcontrol.dialog.switchlightdialog.SwitchLightDialogFragment;
import com.zhida.smartcontrol.pagectrl.lampmanager.editlamp.EditLampFragment;
import com.zhida.smartcontrol.pagectrl.lampmanager.lampdetail.LampDetailFragment;
import com.zhida.smartcontrol.pop.DeviceClassifyPop;

import java.util.List;

import butterknife.BindView;
import butterknife.BindViews;
import butterknife.OnClick;

/**
 * author: tanjianpei
 * date: 2019/10/17 017 15:11
 * description: 双灯控制器
 */
public class DoubleLampFragment extends MVPBaseFragment<DoubleLampContract.View, DoubleLampPresenter> implements DoubleLampContract.View {
    @BindView(R.id.tv_num)
    TextView mTvNum;
    @BindView(R.id.ll_top)
    LinearLayout mTopView;
    @BindView(R.id.rl_bottom_group)
    RelativeLayout mBottom;
    @BindView(R.id.cbox)
    Cbox mCBox;
    @BindView(R.id.cbox_main)
    Cbox mCBoxMain;
    @BindView(R.id.cbox_aux)
    Cbox mCBoxAux;
    @BindView(R.id.recycler_right)
    RecyclerView mRViewRight;
    @BindViews({R.id.fl_kzlx, R.id.fl_tg, R.id.fl_sjjz, R.id.fl_kgd, R.id.fl_nbks})
    List<FrameLayout> mFlBtns;
    @BindViews({R.id.tv_kzlx, R.id.tv_tg, R.id.tv_sjjz, R.id.tv_kgd, R.id.tv_nbks})
    List<TextView> mTvBtns;
    @BindView(R.id.ll_fenlei)
    View mFenleiView;

    private int mWhite = -1;
    private int mBlue = -1;

    private LampBean mBean;
    private DeviceClassifyPop mPop;
    private DoubleLampAdapter mAdapter;

    public static DoubleLampFragment newInstance(LampBean bean) {
        Bundle args = new Bundle();
        args.putParcelable("bean", bean);
        DoubleLampFragment fragment = new DoubleLampFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected Object getLayout() {
        return R.layout.fragment_lamp_single;
    }

    @Override
    protected void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        super.onBindView(savedInstanceState, rootView);
        mWhite = ResUtil.getColor(getProxyActivity(), R.color.c_FFFFFF);
        mBlue = ResUtil.getColor(getProxyActivity(), R.color.c_3862FF);

        initRecycler(mRViewRight, 0.48f, R.color.c_EEEEEE);

        mAdapter = new DoubleLampAdapter(null);
        mAdapter.bindToRecyclerView(mRViewRight);

        mBean = getArguments().getParcelable("bean");

        View view = View.inflate(getProxyActivity(), R.layout.layout_empty_device, null);
        Button btn = view.findViewById(R.id.btn_add_device);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                start(EditLampFragment.newInstance(Const.TYPE_ADD, mBean, null));
            }
        });
        mAdapter.setEmptyView(view);
        mBottom.setVisibility(View.GONE);

        mPop = new DeviceClassifyPop(getProxyActivity(), Const.O_DOUBLE);
    }

    @Override
    protected void initLazy(Bundle savedInstanceState) {

    }

    public void bindData(List<LampSwitchBean> data) {
        for (LampSwitchBean lampBean : data) {
            for (LampSwitchBean datum : mAdapter.getData()) {
                if (datum.getId().equals(lampBean.getId())) {
                    lampBean.setCheck(datum.isCheck());
                    lampBean.setCheckMain(datum.isCheckMain());
                    lampBean.setCheckAux(datum.isCheckAux());
                }
            }
        }

        mAdapter.replaceData(data);

        if (data.size() > 0) {
            mTvNum.setText("共 " + data.size() + " 控制器");
            mTopView.setVisibility(View.VISIBLE);
            mBottom.setVisibility(View.VISIBLE);
        } else {
            mBottom.setVisibility(View.GONE);
            mTopView.setVisibility(View.GONE);
        }
    }

    @Override
    protected void initLazyEvents() {
        mAdapter.setClickListener(new OnItemClick<LampSwitchBean>() {
            @Override
            public void onClick(LampSwitchBean info, int tag) {
                switch (tag) {
                    case 0:
                        start(EditLampFragment.newInstance(Const.TYPE_EDIT, mBean, info));
                        break;
                    case 1:
                        DelDeviceDIalogFragment.newInstance(info.getId()).showDialog(getChildFragmentManager());
                        break;
                    case 2:
                        start(LampDetailFragment.newInstance(info.getId()));
                        break;
                }
            }
        });
        mCBox.setOnCboxClickListener(new Cbox.OnCboxClickListener() {
            @Override
            public void onCboxClick(View v) {
                for (LampSwitchBean bean : mAdapter.getData()) {
                    bean.setCheck(mCBox.isChecked());
                    bean.setCheckMain(bean.isCheck());
                    bean.setCheckAux(bean.isCheck());
                }
                mAdapter.notifyDataSetChanged();
            }
        });
//        mCBoxMain.setOnCboxClickListener(new Cbox.OnCboxClickListener() {
//            @Override
//            public void onCboxClick(View v) {
//                for (LampSwitchBean bean : mAdapter.getData()) {
//                    bean.setCheck(false);
//                    bean.setCheckMain(mCBoxMain.isChecked());
//                    bean.setCheckAux(false);
//                }
//                mAdapter.notifyDataSetChanged();
//            }
//        });
//        mCBoxAux.setOnCboxClickListener(new Cbox.OnCboxClickListener() {
//            @Override
//            public void onCboxClick(View v) {
//                for (LampSwitchBean bean : mAdapter.getData()) {
//                    bean.setCheck(false);
//                    bean.setCheckMain(false);
//                    bean.setCheckAux(mCBoxAux.isChecked());
//                }
//                mAdapter.notifyDataSetChanged();
//            }
//        });
    }

    private void start(BaseFragment fragment) {
        getParentDelegate().start(fragment);
    }

    @OnClick({R.id.fl_kzlx, R.id.fl_tg, R.id.fl_sjjz, R.id.fl_kgd, R.id.fl_nbks, R.id.ll_fenlei})
    public void click(View v) {
        switch (v.getId()) {
            case R.id.ll_fenlei:
                mPop.showAsDropDown(mFenleiView);
                break;
            case R.id.fl_kzlx:
                mPresenter.checkData(mAdapter.getData(), 0);
                break;
            case R.id.fl_tg:
                mPresenter.checkData(mAdapter.getData(), 1);
                break;
            case R.id.fl_sjjz:
                mPresenter.checkData(mAdapter.getData(), 2);
                break;
            case R.id.fl_kgd:
                mPresenter.checkData(mAdapter.getData(), 3);
                break;
            case R.id.fl_nbks:
                mPresenter.checkData(mAdapter.getData(), 4);
                break;
        }
    }

    @Override
    public void checkDataOk(int type, List<LampSwitchBean> beanList) {
        clickBottomBtn(type);
        switch (type) {
            case 0:
                showDialog(LampKzlxDialogFragment.newInstance(Const.NB_DOUBLE, beanList));
                break;
            case 1:
                showDialog(DimmerDialogFragment.newInstance(Const.NB_DOUBLE, beanList));
                break;
            case 2:
                showDialog(LampSjjzDialogFragment.newInstance(Const.NB_DOUBLE, beanList));
                break;
            case 3:
                showDialog(SwitchLightDialogFragment.newInstance(Const.NB_DOUBLE, beanList));
                break;
            case 4:
                showDialog(NBDialogFragment.newInstance(Const.NB_DOUBLE, beanList));
                break;
        }
    }

    private void showDialog(BaseDialogFragment fragment) {
        fragment.showDialog(getChildFragmentManager());
        fragment.setOnDestroyListener(new BaseDialogFragment.OnDestroyListener() {
            @Override
            public void onDestroy(Object object, boolean isChange) {
                resetBtns();
            }
        });
    }

    private void clickBottomBtn(int id) {
        resetBtns();
        mFlBtns.get(id).setEnabled(false);
        mFlBtns.get(id).setBackgroundResource(R.drawable.db_button_1_1);
        mTvBtns.get(id).setTextColor(mWhite);
        mTvBtns.get(id).setBackgroundResource(android.R.color.transparent);
    }

    private void resetBtns() {
        for (FrameLayout mFlBtn : mFlBtns) {
            mFlBtn.setEnabled(true);
            mFlBtn.setBackgroundResource(R.drawable.db_button_2_1);
        }
        for (TextView mTvBtn : mTvBtns) {
            mTvBtn.setTextColor(mBlue);
            mTvBtn.setBackgroundResource(R.drawable.s_bg_search);
        }
    }


}
